package com.itheima.Test01.edu.info.manager.teacher.controller;

import com.itheima.Test01.edu.info.manager.teacher.domain.Student;
import com.itheima.Test01.edu.info.manager.teacher.service.StudentSercice;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    public void start() {

        while (true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int choice = sc.nextInt();
            switch (choice){
                case 1:
                    addStudent();
                    break;
                case 2:
                    deletStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    queryStudent();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入错误");

            }
        }
    }

    public void updateStudent() {
        System.out.println("输入学号");
        String id = sc.next();
        boolean exists = StudentSercice.isExists(id);
        if (exists){
            System.out.println("不存在");
            return;
        }
        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
        System.out.println("输入住址");
        String address = sc.next();
        Student stu = new Student(id,name,age,address);
        StudentSercice.updateStudent(stu,id);

    }

    public void deletStudent() {
        System.out.println("输入学号");
        String id = sc.next();
        boolean exists = StudentSercice.isExists(id);
        if (exists){
            System.out.println("不存在");
            return;
        }
        StudentSercice.deletStudent(id);
        System.out.println("输入成功!!!!");


    }

    public void queryStudent() {
        Student[] stu = StudentSercice.QueryStudent();
        if (stu == null){
            System.out.println("未信息查询");
            return;
        }
        /*for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null){
                break;
            }
            if (i == stu.length-1){
                System.out.println("未信息查询");
                return;
            }
        }*/
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null){
                System.out.println(stu[i].getId()+"\t"+stu[i].getName()+"\t"+stu[i].getAge()+"\t"+stu[i].getAddress());
            }
        }
    }

    public void addStudent() {
        String id;
        while (true){
            System.out.println("输入学号");
            id = sc.next();
            boolean exists = StudentSercice.isExists(id);
            if (exists){
                break;
            }else {
                System.out.println("学号已存在");
            }
        }
        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
        System.out.println("输入住址");
        String address = sc.next();
        Student stu = new Student(id,name,age,address);
        boolean flag = StudentSercice.addStudent(stu);
        if (flag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败,存储已满");
        }

    }
}
